Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 870)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.03686 13.03259 13.02837 13.02419 13.02006 13.01597 13.01193 13.00794
##   [9] 13.00400 13.00010 12.99625 12.99244 12.98869 12.98497 12.98131 12.97769
##  [17] 12.97411 12.97059 12.96710 12.96367 12.96028 12.95694 12.95364 12.95039
##  [25] 12.94718 12.94402 12.94091 12.93784 12.93482 12.93184 12.92891 12.92602
##  [33] 12.92318 12.92039 12.91764 12.91493 12.91227 12.90966 12.90709 12.90456
##  [41] 12.90209 12.89965 12.89726 12.89493 12.89267 12.89047 12.88835 12.88629
##  [49] 12.88429 12.88236 12.88050 12.87870 12.87696 12.87528 12.87367 12.87212
##  [57] 12.87063 12.86920 12.86782 12.86651 12.86525 12.86405 12.86291 12.86182
##  [65] 12.86079 12.85981 12.85889 12.85802 12.85720 12.85643 12.85571 12.85505
##  [73] 12.85443 12.85386 12.85334 12.85287 12.85244 12.85206 12.85173 12.85144
##  [81] 12.85120 12.85099 12.85083 12.85071 12.85064 12.85060 12.85061 12.85065
##  [89] 12.85073 12.85085 12.85101 12.85120 12.85145 12.85179 12.85222 12.85272
##  [97] 12.85331 12.85397 12.85471 12.85551 12.85639 12.85734 12.85835 12.85943
## [105] 12.86057 12.86176 12.86302 12.86432 12.86569 12.86710 12.86855 12.87006
## [113] 12.87161 12.87319 12.87482 12.87649 12.87819 12.87992 12.88168 12.88347
## [121] 12.88528 12.88712 12.88898 12.89086 12.89275 12.89466 12.89658 12.89851
## [129] 12.90045 12.90240 12.90434 12.90629 12.90824 12.91018 12.91212 12.91478
## [137] 12.91881 12.92410 12.93053 12.93799 12.94637 12.95555 12.96541 12.97586
## [145] 12.98676 12.99801 13.00949 13.02109 13.03270 13.04419 13.05547 13.06641
## [153] 13.07689 13.08682 13.09606 13.10451 13.11206 13.11858 13.12397 13.12812
## [161] 13.13090 13.13336 13.13657 13.14047 13.14501 13.15012 13.15575 13.16185
## [169] 13.16834 13.17518 13.18230 13.18965 13.19716 13.20479 13.21247 13.22014
## [177] 13.22775 13.23523 13.24253 13.24960 13.25637 13.26278 13.26877 13.27430
## [185] 13.27929 13.28369 13.28745 13.29050 13.29279 13.29425 13.29484 13.29448
## [193] 13.29313 13.29073 13.28721 13.28251 13.27646 13.26897 13.26015 13.25011
## [201] 13.23896 13.22680 13.21373 13.19988 13.18535 13.17024 13.15466 13.13872
## [209] 13.12253 13.10619 13.08982 13.07352 13.05740 13.04156 13.02612 13.01118
## [217] 12.99685 12.98324 12.97045 12.95698 12.94134 12.92372 12.90426 12.88316
## [225] 12.86056 12.83664 12.81156 12.78550 12.75862 12.73108 12.70307 12.67473
## [233] 12.64625 12.61779 12.58951 12.56159 12.53419 12.50749 12.48163 12.45681
## [241] 12.43317 12.41090 12.39015 12.37110 12.35391 12.33764 12.32126 12.30479
## [249] 12.28824 12.27165 12.25503 12.23842 12.22182 12.20528 12.18880 12.17241
## [257] 12.15614 12.14001 12.12404 12.10826 12.09268 12.07733 12.06224 12.04743
## [265] 12.03292 12.01873 12.00489 11.99142 11.97872 11.96710 11.95651 11.94685
## [273] 11.93805 11.93003 11.92273 11.91604 11.90991 11.90426 11.89900 11.89406
## [281] 11.88936 11.88482 11.88037 11.87593 11.87142 11.86676 11.86188 11.85670
## [289] 11.85114 11.84512 11.83858 11.83142 11.82357 11.81496 11.80561 11.79565
## [297] 11.78516 11.77420 11.76286 11.75121 11.73932 11.72727 11.71513 11.70298
## [305] 11.69089 11.67893 11.66719 11.65573 11.64463 11.63397 11.62381 11.61424
## [313] 11.60532 11.59714 11.58976 11.58327 11.57773 11.57206 11.56523 11.55736
## [321] 11.54856 11.53898 11.52872 11.51792 11.50671 11.49520 11.48352 11.47181
## [329] 11.46017 11.44875 11.43765 11.42702 11.41697 11.40763 11.39912 11.39157
## [337] 11.38511 11.37986 11.37594 11.37349 11.37261 11.37345 11.37613 11.38012
## [345] 11.38481 11.39016 11.39615 11.40275 11.40995 11.41770 11.42599 11.43479
## [353] 11.44407 11.45381 11.46397 11.47454 11.48548 11.49678 11.50840 11.52032
## [361] 11.53251 11.54494 11.55759 11.57043 11.58344 11.59659 11.61144 11.62943
## [369] 11.65030 11.67384 11.69980 11.72795 11.75806 11.78990 11.82322 11.85779
## [377] 11.89339 11.92977 11.96670 12.00394 12.04127 12.07845 12.11524 12.15141
## [385] 12.18672 12.22094 12.25384 12.28518 12.31473 12.34224 12.36750 12.39026
## [393] 12.41029 12.42736 12.44340 12.46044 12.47835 12.49704 12.51638 12.53626
## [401] 12.55657 12.57718 12.59800 12.61889 12.63976 12.66048 12.68094 12.70102
## [409] 12.72062 12.73961 12.75789 12.77534 12.79184 12.80729 12.82156 12.83454
## [417] 12.84612 12.85619 12.86463 12.87132 12.87610 12.87899 12.88011 12.87961
## [425] 12.87765 12.87436 12.86988 12.86436 12.85794 12.85077 12.84299 12.83474
## [433] 12.82617 12.81741 12.80863 12.79995 12.79152 12.78348 12.77598 12.76917
## [441] 12.76318 12.75816 12.75425 12.74956 12.74227 12.73264 12.72093 12.70741
## [449] 12.69233 12.67596 12.65855 12.64037 12.62168 12.60273 12.58380 12.56513
## [457] 12.54700 12.52966 12.51337 12.49839 12.48499 12.47343 12.46396 12.45685
## [465] 12.45023 12.44218 12.43283 12.42231 12.41075 12.39830 12.38509 12.37126
## [473] 12.35694 12.34226 12.32737 12.31240 12.29748 12.28276 12.26836 12.25442
## [481] 12.24108 12.22848 12.21674 12.20602 12.19643 12.18812 12.18122 12.17588
## [489] 12.17221 12.17037 12.17040 12.17215 12.17553 12.18040 12.18664 12.19415
## [497] 12.20278 12.21244 12.22299 12.23432 12.24630 12.25882 12.27176 12.28499
## [505] 12.29841 12.31187 12.32528 12.33850 12.35142 12.36392 12.37587 12.38716
## [513] 12.39767 12.40728 12.41586 12.42331 12.42949 12.43429 12.43931 12.44613
## [521] 12.45460 12.46456 12.47585 12.48832 12.50181 12.51616 12.53121 12.54682
## [529] 12.56282 12.57906 12.59537 12.61161 12.62761 12.64323 12.65829 12.67266
## [537] 12.68616 12.69865 12.70996 12.71994 12.72844 12.73529 12.74034 12.74343
## [545] 12.74441 12.74312 12.73991 12.73531 12.72938 12.72224 12.71396 12.70463
## [553] 12.69434 12.68319 12.67125 12.65863 12.64540 12.63165 12.61748 12.60298
## [561] 12.58822 12.57331 12.55832 12.54336 12.52850 12.51383 12.49945 12.48545
## [569] 12.47190 12.45891 12.44655 12.43492 12.42411 12.41421 12.40350 12.39040
## [577] 12.37512 12.35790 12.33895 12.31851 12.29681 12.27407 12.25053 12.22640
## [585] 12.20192 12.17731 12.15281 12.12864 12.10503 12.08220 12.06038 12.03981
## [593] 12.02071 12.00330 11.98782 11.97450 11.96355 11.95302 11.94091 11.92738
## [601] 11.91257 11.89663 11.87973 11.86202 11.84365 11.82478 11.80556 11.78614
## [609] 11.76668 11.74733 11.72825 11.70960 11.69152 11.67417 11.65770 11.64227
## [617] 11.62804 11.61515 11.60376 11.59403 11.58611 11.58015 11.57631 11.57455
## [625] 11.57463 11.57643 11.57981 11.58464 11.59080 11.59814 11.60654 11.61586
## [633] 11.62598 11.63676 11.64808 11.65979 11.67177 11.68389 11.69602 11.70802
## [641] 11.71976 11.73111 11.74195 11.75213 11.76153 11.77001 11.77907 11.79019
## [649] 11.80321 11.81796 11.83430 11.85206 11.87109 11.89123 11.91232 11.93420
## [657] 11.95672 11.97972 12.00303 12.02651 12.04999 12.07331 12.09632 12.11887
## [665] 12.14078 12.16191 12.18210 12.20119 12.21901 12.23542 12.25026 12.26336
## [673] 12.27577 12.28861 12.30183 12.31538 12.32924 12.34336 12.35769 12.37218
## [681] 12.38681 12.40153 12.41629 12.43105 12.44578 12.46042 12.47493 12.48928
## [689] 12.50342 12.51731 12.53091 12.54416 12.55704 12.56950 12.58150 12.59299
## [697] 12.60393 12.61429 12.62401 12.63305 12.64138 12.64902 12.65607 12.66256
## [705] 12.66855 12.67408 12.67919 12.68393 12.68835 12.69249 12.69640 12.70011
## [713] 12.70369 12.70716 12.71059 12.71400 12.71746 12.72100 12.72467 12.72851
## [721] 12.73258 12.73691 12.74155 12.74624 12.75068 12.75489 12.75888 12.76266
## [729] 12.76623 12.76960 12.77278 12.77579 12.77863 12.78131 12.78384 12.78623
## [737] 12.78849 12.79063 12.79265 12.79457 12.79639 12.79813 12.79980 12.80139
## [745] 12.80293 12.80442 12.80588 12.80730 12.80870 12.80995 12.81092 12.81164
## [753] 12.81212 12.81237 12.81242 12.81227 12.81195 12.81148 12.81087 12.81013
## [761] 12.80929 12.80836 12.80736 12.80630 12.80521 12.80410 12.80298 12.80188
## [769] 12.80081 12.79978 12.79882 12.79794 12.79699 12.79584 12.79448 12.79294
## [777] 12.79122 12.78936 12.78735 12.78522 12.78297 12.78063 12.77821 12.77571
## [785] 12.77317 12.77058 12.76797 12.76535 12.76274 12.76014 12.75758 12.75507
## [793] 12.75262 12.75025 12.74797 12.74579 12.74374 12.74183 12.73996 12.73806
## [801] 12.73612 12.73414 12.73212 12.73007 12.72798 12.72585 12.72368 12.72148
## [809] 12.71924 12.71697 12.71466 12.71232 12.70994 12.70752 12.70507 12.70259
## [817] 12.70007 12.69752 12.69494 12.69232 12.68967 12.68699 12.68428 12.68153
## [825] 12.67875 12.67594 12.67310 12.67021 12.66727 12.66430 12.66128 12.65823
## [833] 12.65514 12.65201 12.64884 12.64564 12.64240 12.63913 12.63583 12.63250
## [841] 12.62914 12.62575 12.62234 12.61889 12.61542 12.61193 12.60841 12.60487
## [849] 12.60131 12.59774 12.59415 12.59055 12.58694 12.58332 12.57968 12.57602
## [857] 12.57234 12.56865 12.56493 12.56119 12.55743 12.55364 12.54982 12.54598
## [865] 12.54210 12.53820 12.53426 12.53029 12.52628 12.52224
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 870)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.58198 12.57904 12.57615 12.57333 12.57057 12.56787 12.56524 12.56267
##   [9] 12.56016 12.55772 12.55534 12.55303 12.55078 12.54860 12.54649 12.54444
##  [17] 12.54246 12.54055 12.53871 12.53693 12.53522 12.53359 12.53202 12.53052
##  [25] 12.52910 12.52774 12.52646 12.52524 12.52410 12.52304 12.52204 12.52112
##  [33] 12.52027 12.51950 12.51880 12.51818 12.51763 12.51716 12.51676 12.51644
##  [41] 12.51620 12.51603 12.51595 12.51595 12.51605 12.51625 12.51655 12.51695
##  [49] 12.51744 12.51802 12.51870 12.51946 12.52032 12.52126 12.52229 12.52340
##  [57] 12.52460 12.52588 12.52724 12.52868 12.53019 12.53179 12.53346 12.53520
##  [65] 12.53701 12.53890 12.54085 12.54288 12.54497 12.54712 12.54934 12.55162
##  [73] 12.55396 12.55636 12.55882 12.56134 12.56391 12.56654 12.56922 12.57195
##  [81] 12.57473 12.57756 12.58043 12.58336 12.58632 12.58933 12.59238 12.59547
##  [89] 12.59860 12.60176 12.60496 12.60820 12.61162 12.61536 12.61940 12.62374
##  [97] 12.62836 12.63323 12.63836 12.64371 12.64929 12.65507 12.66103 12.66717
## [105] 12.67347 12.67992 12.68649 12.69318 12.69997 12.70684 12.71379 12.72079
## [113] 12.72783 12.73490 12.74198 12.74906 12.75612 12.76314 12.77012 12.77704
## [121] 12.78388 12.79063 12.79728 12.80380 12.81019 12.81643 12.82250 12.82839
## [129] 12.83409 12.83958 12.84484 12.84987 12.85464 12.85979 12.86588 12.87283
## [137] 12.88052 12.88887 12.89779 12.90716 12.91690 12.92691 12.93709 12.94734
## [145] 12.95758 12.96769 12.97759 12.98718 12.99636 13.00503 13.01310 13.02047
## [153] 13.02704 13.03273 13.03742 13.04102 13.04463 13.04936 13.05512 13.06183
## [161] 13.06940 13.07776 13.08681 13.09647 13.10666 13.11729 13.12828 13.13955
## [169] 13.15101 13.16258 13.17418 13.18571 13.19710 13.20827 13.21912 13.22957
## [177] 13.23955 13.24897 13.25773 13.26577 13.27299 13.27931 13.28465 13.28892
## [185] 13.29204 13.29392 13.29449 13.29365 13.29132 13.28725 13.28130 13.27360
## [193] 13.26427 13.25342 13.24118 13.22767 13.21300 13.19729 13.18068 13.16326
## [201] 13.14518 13.12654 13.10746 13.08808 13.06849 13.04884 13.02922 13.00978
## [209] 12.99062 12.97186 12.95363 12.93604 12.91922 12.90329 12.88836 12.87455
## [217] 12.86199 12.84886 12.83339 12.81581 12.79634 12.77517 12.75255 12.72866
## [225] 12.70375 12.67801 12.65166 12.62493 12.59802 12.57115 12.54454 12.51840
## [233] 12.49295 12.46840 12.44497 12.42288 12.40233 12.38355 12.36675 12.35215
## [241] 12.33891 12.32605 12.31355 12.30138 12.28954 12.27800 12.26673 12.25573
## [249] 12.24497 12.23443 12.22409 12.21394 12.20395 12.19411 12.18439 12.17478
## [257] 12.16525 12.15579 12.14638 12.13700 12.12762 12.11824 12.10883 12.09936
## [265] 12.08983 12.08021 12.07135 12.06401 12.05805 12.05335 12.04977 12.04718
## [273] 12.04543 12.04441 12.04396 12.04397 12.04429 12.04480 12.04535 12.04582
## [281] 12.04606 12.04595 12.04536 12.04414 12.04216 12.03929 12.03540 12.03035
## [289] 12.02401 12.01686 12.00949 12.00192 11.99417 11.98627 11.97824 11.97009
## [297] 11.96186 11.95357 11.94524 11.93688 11.92853 11.92021 11.91193 11.90373
## [305] 11.89561 11.88762 11.87976 11.87206 11.86455 11.85724 11.85016 11.84332
## [313] 11.83677 11.83051 11.82456 11.81763 11.80856 11.79755 11.78486 11.77069
## [321] 11.75527 11.73885 11.72163 11.70384 11.68573 11.66750 11.64939 11.63162
## [329] 11.61442 11.59802 11.58264 11.56851 11.55586 11.54492 11.53590 11.52904
## [337] 11.52457 11.52271 11.52229 11.52203 11.52194 11.52205 11.52237 11.52293
## [345] 11.52374 11.52484 11.52624 11.52797 11.53004 11.53248 11.53530 11.53853
## [353] 11.54220 11.54632 11.55091 11.55600 11.56160 11.56775 11.57445 11.58174
## [361] 11.58963 11.59814 11.60730 11.61713 11.62873 11.64306 11.65993 11.67914
## [369] 11.70049 11.72378 11.74883 11.77543 11.80339 11.83250 11.86258 11.89343
## [377] 11.92486 11.95665 11.98863 12.02059 12.05234 12.08367 12.11440 12.14433
## [385] 12.17326 12.20099 12.22733 12.25208 12.27505 12.29604 12.31485 12.33129
## [393] 12.34762 12.36612 12.38652 12.40862 12.43217 12.45693 12.48269 12.50919
## [401] 12.53622 12.56353 12.59090 12.61808 12.64485 12.67098 12.69623 12.72036
## [409] 12.74315 12.76435 12.78375 12.80110 12.81617 12.82873 12.83854 12.84641
## [417] 12.85329 12.85924 12.86430 12.86850 12.87190 12.87453 12.87645 12.87768
## [425] 12.87827 12.87826 12.87771 12.87664 12.87510 12.87314 12.87079 12.86810
## [433] 12.86511 12.86187 12.85841 12.85477 12.85101 12.84716 12.84326 12.83937
## [441] 12.83551 12.83003 12.82145 12.81005 12.79612 12.77994 12.76181 12.74200
## [449] 12.72081 12.69853 12.67543 12.65182 12.62797 12.60417 12.58072 12.55789
## [457] 12.53598 12.51527 12.49605 12.47860 12.46323 12.45020 12.43981 12.43235
## [465] 12.42564 12.41740 12.40778 12.39693 12.38500 12.37212 12.35846 12.34414
## [473] 12.32933 12.31417 12.29880 12.28337 12.26802 12.25291 12.23818 12.22398
## [481] 12.21045 12.19773 12.18599 12.17536 12.16599 12.15802 12.15160 12.14689
## [489] 12.14402 12.14314 12.14409 12.14655 12.15042 12.15560 12.16199 12.16950
## [497] 12.17803 12.18749 12.19777 12.20877 12.22041 12.23259 12.24520 12.25815
## [505] 12.27134 12.28468 12.29807 12.31141 12.32460 12.33755 12.35016 12.36234
## [513] 12.37398 12.38499 12.39526 12.40472 12.41325 12.42076 12.42926 12.44068
## [521] 12.45476 12.47128 12.48999 12.51066 12.53304 12.55690 12.58200 12.60809
## [529] 12.63495 12.66233 12.69000 12.71770 12.74522 12.77230 12.79870 12.82420
## [537] 12.84855 12.87150 12.89283 12.91230 12.92966 12.94467 12.95710 12.96672
## [545] 12.97327 12.97652 12.97744 12.97719 12.97583 12.97342 12.97002 12.96568
## [553] 12.96046 12.95442 12.94761 12.94009 12.93193 12.92317 12.91387 12.90410
## [561] 12.89390 12.88334 12.87248 12.86136 12.85006 12.83861 12.82710 12.81556
## [569] 12.80406 12.79265 12.78140 12.77035 12.75957 12.74912 12.73705 12.72157
## [577] 12.70301 12.68167 12.65787 12.63191 12.60412 12.57479 12.54425 12.51281
## [585] 12.48077 12.44845 12.41616 12.38422 12.35292 12.32260 12.29355 12.26609
## [593] 12.24054 12.21719 12.19637 12.17839 12.16356 12.14923 12.13269 12.11414
## [601] 12.09379 12.07184 12.04852 12.02402 11.99854 11.97231 11.94552 11.91838
## [609] 11.89111 11.86390 11.83696 11.81051 11.78475 11.75988 11.73612 11.71367
## [617] 11.69274 11.67353 11.65626 11.64113 11.62835 11.61813 11.61066 11.60538
## [625] 11.60149 11.59893 11.59760 11.59743 11.59833 11.60022 11.60303 11.60667
## [633] 11.61106 11.61611 11.62175 11.62790 11.63448 11.64139 11.64857 11.65593
## [641] 11.66339 11.67086 11.67827 11.68554 11.69258 11.69932 11.70721 11.71765
## [649] 11.73044 11.74541 11.76236 11.78110 11.80145 11.82322 11.84622 11.87027
## [657] 11.89517 11.92074 11.94679 11.97313 11.99958 12.02594 12.05203 12.07766
## [665] 12.10265 12.12680 12.14993 12.17185 12.19237 12.21130 12.22846 12.24365
## [673] 12.25826 12.27374 12.29001 12.30700 12.32465 12.34288 12.36163 12.38082
## [681] 12.40039 12.42026 12.44036 12.46063 12.48100 12.50139 12.52173 12.54196
## [689] 12.56201 12.58180 12.60126 12.62033 12.63893 12.65700 12.67446 12.69125
## [697] 12.70729 12.72251 12.73685 12.75024 12.76260 12.77421 12.78539 12.79617
## [705] 12.80657 12.81662 12.82635 12.83576 12.84489 12.85377 12.86240 12.87083
## [713] 12.87906 12.88713 12.89506 12.90287 12.91058 12.91823 12.92582 12.93339
## [721] 12.94096 12.94855 12.95618 12.96383 12.97143 12.97897 12.98643 12.99381
## [729] 13.00108 13.00824 13.01528 13.02217 13.02891 13.03549 13.04188 13.04808
## [737] 13.05408 13.05985 13.06539 13.07069 13.07573 13.08049 13.08497 13.08915
## [745] 13.09302 13.09656 13.09977 13.10262 13.10511 13.10721 13.10892 13.11026
## [753] 13.11126 13.11193 13.11229 13.11237 13.11218 13.11174 13.11108 13.11022
## [761] 13.10917 13.10796 13.10660 13.10513 13.10355 13.10189 13.10017 13.09840
## [769] 13.09662 13.09484 13.09308 13.09136 13.08934 13.08667 13.08342 13.07964
## [777] 13.07536 13.07064 13.06553 13.06008 13.05434 13.04835 13.04217 13.03584
## [785] 13.02941 13.02294 13.01646 13.01004 13.00371 12.99753 12.99155 12.98582
## [793] 12.98037 12.97528 12.97057 12.96631 12.96253 12.95930 12.95628 12.95314
## [801] 12.94988 12.94650 12.94302 12.93945 12.93579 12.93205 12.92824 12.92437
## [809] 12.92044 12.91647 12.91247 12.90844 12.90439 12.90032 12.89626 12.89220
## [817] 12.88815 12.88413 12.88013 12.87618 12.87227 12.86843 12.86464 12.86093
## [825] 12.85730 12.85376 12.85024 12.84667 12.84305 12.83939 12.83569 12.83196
## [833] 12.82821 12.82443 12.82064 12.81684 12.81303 12.80922 12.80541 12.80161
## [841] 12.79783 12.79406 12.79032 12.78660 12.78292 12.77928 12.77568 12.77213
## [849] 12.76863 12.76522 12.76190 12.75868 12.75555 12.75249 12.74951 12.74660
## [857] 12.74375 12.74095 12.73820 12.73550 12.73282 12.73018 12.72755 12.72494
## [865] 12.72234 12.71974 12.71713 12.71451 12.71188 12.70922
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 870)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.03021 12.02423 12.01834 12.01254 12.00683 12.00121 11.99568 11.99023
##   [9] 11.98488 11.97962 11.97444 11.96935 11.96435 11.95944 11.95462 11.94989
##  [17] 11.94524 11.94068 11.93622 11.93184 11.92754 11.92334 11.91923 11.91520
##  [25] 11.91126 11.90741 11.90364 11.89997 11.89638 11.89288 11.88947 11.88614
##  [33] 11.88290 11.87975 11.87669 11.87372 11.87083 11.86803 11.86531 11.86269
##  [41] 11.86015 11.85770 11.85533 11.85305 11.85086 11.84876 11.84674 11.84481
##  [49] 11.84296 11.84120 11.83956 11.83804 11.83666 11.83541 11.83428 11.83329
##  [57] 11.83241 11.83166 11.83103 11.83052 11.83012 11.82984 11.82968 11.82962
##  [65] 11.82968 11.82984 11.83011 11.83048 11.83095 11.83153 11.83220 11.83298
##  [73] 11.83384 11.83480 11.83585 11.83699 11.83822 11.83953 11.84093 11.84241
##  [81] 11.84397 11.84561 11.84733 11.84912 11.85098 11.85292 11.85493 11.85700
##  [89] 11.85914 11.86134 11.86360 11.86593 11.86831 11.87075 11.87324 11.87579
##  [97] 11.87839 11.88104 11.88373 11.88652 11.88945 11.89251 11.89571 11.89903
## [105] 11.90249 11.90608 11.90979 11.91363 11.91759 11.92167 11.92587 11.93018
## [113] 11.93461 11.93915 11.94381 11.94857 11.95344 11.95842 11.96349 11.96868
## [121] 11.97395 11.97933 11.98480 11.99037 11.99603 12.00178 12.00761 12.01354
## [129] 12.01954 12.02563 12.03180 12.03805 12.04437 12.05077 12.05724 12.06491
## [137] 12.07479 12.08669 12.10045 12.11588 12.13281 12.15108 12.17049 12.19087
## [145] 12.21205 12.23386 12.25611 12.27863 12.30125 12.32378 12.34606 12.36790
## [153] 12.38913 12.40958 12.42907 12.44742 12.46446 12.48000 12.49389 12.50593
## [161] 12.51596 12.52533 12.53550 12.54638 12.55792 12.57004 12.58268 12.59576
## [169] 12.60923 12.62300 12.63701 12.65120 12.66549 12.67982 12.69412 12.70831
## [177] 12.72234 12.73613 12.74961 12.76272 12.77539 12.78755 12.79912 12.81005
## [185] 12.82027 12.82970 12.83828 12.84593 12.85260 12.85820 12.86269 12.86597
## [193] 12.86800 12.86869 12.86798 12.86580 12.86145 12.85442 12.84491 12.83316
## [201] 12.81938 12.80378 12.78659 12.76802 12.74829 12.72762 12.70623 12.68433
## [209] 12.66215 12.63990 12.61779 12.59606 12.57491 12.55456 12.53524 12.51716
## [217] 12.50053 12.48558 12.47253 12.45945 12.44439 12.42749 12.40893 12.38884
## [225] 12.36739 12.34474 12.32103 12.29642 12.27107 12.24513 12.21876 12.19211
## [233] 12.16534 12.13860 12.11205 12.08584 12.06013 12.03508 12.01083 11.98755
## [241] 11.96539 11.94450 11.92504 11.90716 11.89103 11.87592 11.86101 11.84629
## [249] 11.83175 11.81739 11.80319 11.78914 11.77524 11.76148 11.74784 11.73432
## [257] 11.72091 11.70760 11.69439 11.68125 11.66819 11.65519 11.64224 11.62934
## [265] 11.61648 11.60364 11.59083 11.57802 11.56551 11.55358 11.54218 11.53131
## [273] 11.52091 11.51096 11.50143 11.49229 11.48351 11.47505 11.46688 11.45898
## [281] 11.45131 11.44384 11.43654 11.42937 11.42232 11.41534 11.40840 11.40147
## [289] 11.39453 11.38754 11.38047 11.37328 11.36596 11.35846 11.35080 11.34304
## [297] 11.33521 11.32733 11.31944 11.31157 11.30373 11.29597 11.28831 11.28079
## [305] 11.27342 11.26623 11.25927 11.25255 11.24611 11.23996 11.23416 11.22871
## [313] 11.22365 11.21901 11.21482 11.21111 11.20790 11.20423 11.19920 11.19295
## [321] 11.18561 11.17733 11.16823 11.15845 11.14813 11.13741 11.12642 11.11530
## [329] 11.10418 11.09320 11.08250 11.07221 11.06247 11.05342 11.04518 11.03791
## [337] 11.03173 11.02678 11.02320 11.02112 11.02068 11.02202 11.02527 11.03026
## [345] 11.03666 11.04438 11.05332 11.06338 11.07448 11.08651 11.09938 11.11300
## [353] 11.12727 11.14209 11.15737 11.17301 11.18892 11.20501 11.22117 11.23732
## [361] 11.25335 11.26918 11.28470 11.29982 11.31445 11.32849 11.34354 11.36114
## [369] 11.38110 11.40323 11.42732 11.45320 11.48065 11.50951 11.53956 11.57062
## [377] 11.60249 11.63498 11.66790 11.70105 11.73425 11.76729 11.79999 11.83215
## [385] 11.86359 11.89410 11.92349 11.95157 11.97815 12.00304 12.02604 12.04695
## [393] 12.06559 12.08177 12.09695 12.11270 12.12894 12.14559 12.16257 12.17982
## [401] 12.19726 12.21482 12.23241 12.24996 12.26741 12.28467 12.30166 12.31833
## [409] 12.33458 12.35034 12.36554 12.38011 12.39396 12.40703 12.41924 12.43052
## [417] 12.44078 12.44996 12.45797 12.46475 12.47033 12.47484 12.47836 12.48094
## [425] 12.48265 12.48357 12.48377 12.48331 12.48226 12.48069 12.47866 12.47626
## [433] 12.47354 12.47057 12.46743 12.46418 12.46088 12.45762 12.45445 12.45145
## [441] 12.44868 12.44622 12.44412 12.44110 12.43593 12.42883 12.42000 12.40965
## [449] 12.39799 12.38523 12.37157 12.35722 12.34239 12.32730 12.31213 12.29711
## [457] 12.28245 12.26834 12.25500 12.24263 12.23145 12.22165 12.21346 12.20707
## [465] 12.20073 12.19266 12.18301 12.17194 12.15961 12.14617 12.13178 12.11660
## [473] 12.10079 12.08450 12.06789 12.05111 12.03433 12.01770 12.00138 11.98553
## [481] 11.97029 11.95584 11.94233 11.92991 11.91874 11.90898 11.90079 11.89432
## [489] 11.88973 11.88718 11.88622 11.88626 11.88724 11.88910 11.89179 11.89524
## [497] 11.89941 11.90423 11.90963 11.91558 11.92200 11.92884 11.93604 11.94354
## [505] 11.95128 11.95922 11.96728 11.97541 11.98355 11.99164 11.99963 12.00746
## [513] 12.01507 12.02240 12.02939 12.03599 12.04214 12.04777 12.05420 12.06268
## [521] 12.07303 12.08509 12.09870 12.11369 12.12989 12.14715 12.16530 12.18416
## [529] 12.20359 12.22341 12.24345 12.26356 12.28357 12.30331 12.32263 12.34134
## [537] 12.35930 12.37633 12.39227 12.40696 12.42022 12.43190 12.44184 12.44986
## [545] 12.45580 12.45950 12.46187 12.46396 12.46576 12.46727 12.46851 12.46948
## [553] 12.47017 12.47059 12.47076 12.47066 12.47030 12.46969 12.46884 12.46773
## [561] 12.46639 12.46481 12.46300 12.46095 12.45868 12.45619 12.45347 12.45054
## [569] 12.44740 12.44406 12.44050 12.43675 12.43280 12.42866 12.42334 12.41598
## [577] 12.40675 12.39584 12.38343 12.36970 12.35484 12.33902 12.32243 12.30526
## [585] 12.28767 12.26986 12.25201 12.23429 12.21690 12.20001 12.18380 12.16846
## [593] 12.15416 12.14110 12.12945 12.11939 12.11111 12.10271 12.09230 12.08004
## [601] 12.06613 12.05073 12.03402 12.01619 11.99740 11.97784 11.95769 11.93711
## [609] 11.91630 11.89542 11.87466 11.85418 11.83418 11.81482 11.79629 11.77876
## [617] 11.76241 11.74741 11.73395 11.72220 11.71234 11.70455 11.69900 11.69527
## [625] 11.69275 11.69136 11.69102 11.69165 11.69317 11.69551 11.69857 11.70228
## [633] 11.70657 11.71134 11.71651 11.72202 11.72777 11.73369 11.73970 11.74571
## [641] 11.75165 11.75743 11.76298 11.76821 11.77304 11.77740 11.78242 11.78921
## [649] 11.79764 11.80756 11.81884 11.83135 11.84495 11.85950 11.87487 11.89092
## [657] 11.90752 11.92453 11.94181 11.95922 11.97664 11.99393 12.01094 12.02755
## [665] 12.04361 12.05900 12.07358 12.08720 12.09974 12.11105 12.12101 12.12947
## [673] 12.13729 12.14539 12.15375 12.16233 12.17111 12.18007 12.18918 12.19841
## [681] 12.20773 12.21712 12.22656 12.23601 12.24545 12.25485 12.26420 12.27345
## [689] 12.28258 12.29158 12.30040 12.30903 12.31744 12.32560 12.33348 12.34107
## [697] 12.34833 12.35523 12.36175 12.36787 12.37355 12.37874 12.38340 12.38759
## [705] 12.39135 12.39472 12.39774 12.40045 12.40290 12.40514 12.40720 12.40912
## [713] 12.41096 12.41275 12.41453 12.41636 12.41826 12.42029 12.42248 12.42489
## [721] 12.42754 12.43049 12.43378 12.43720 12.44050 12.44369 12.44678 12.44976
## [729] 12.45263 12.45540 12.45808 12.46066 12.46315 12.46555 12.46786 12.47009
## [737] 12.47224 12.47431 12.47630 12.47822 12.48007 12.48185 12.48356 12.48521
## [745] 12.48681 12.48834 12.48982 12.49125 12.49263 12.49398 12.49531 12.49663
## [753] 12.49792 12.49917 12.50040 12.50159 12.50273 12.50383 12.50488 12.50587
## [761] 12.50680 12.50766 12.50846 12.50917 12.50982 12.51037 12.51084 12.51122
## [769] 12.51150 12.51168 12.51175 12.51171 12.51152 12.51116 12.51063 12.50995
## [777] 12.50913 12.50819 12.50712 12.50595 12.50469 12.50335 12.50194 12.50047
## [785] 12.49896 12.49741 12.49584 12.49427 12.49270 12.49114 12.48961 12.48811
## [793] 12.48667 12.48529 12.48399 12.48277 12.48165 12.48064 12.47966 12.47863
## [801] 12.47754 12.47641 12.47523 12.47400 12.47273 12.47142 12.47006 12.46867
## [809] 12.46724 12.46577 12.46426 12.46273 12.46116 12.45957 12.45795 12.45630
## [817] 12.45463 12.45294 12.45122 12.44949 12.44774 12.44598 12.44420 12.44241
## [825] 12.44061 12.43880 12.43696 12.43508 12.43316 12.43120 12.42920 12.42716
## [833] 12.42509 12.42299 12.42085 12.41869 12.41650 12.41429 12.41205 12.40979
## [841] 12.40751 12.40522 12.40291 12.40059 12.39825 12.39591 12.39356 12.39120
## [849] 12.38884 12.38649 12.38415 12.38183 12.37952 12.37722 12.37492 12.37263
## [857] 12.37034 12.36805 12.36575 12.36345 12.36114 12.35882 12.35648 12.35412
## [865] 12.35174 12.34935 12.34692 12.34447 12.34199 12.33948
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")